// pages/afterPart/afterPart.js
const app = getApp();
import CommonService from '../../utils/service/commonService.js';
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
const util = require('../../utils/util');
import Base64 from '../../utils/Base64.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //传参来的信息
    paramObj:null,
    //列表
    list:[],
    //页码
    page: 1,
    //是否无更多
    notMore: true,
    //无数据
    showNothing:false,
    //搜索关键词
    searchValue:'',
    //删除确认框
    showDelete:false,
    //删除的id
    deleteId:null,
    //删除的名称
    deleteName:null,
    //服务详情
    serviceDetails:[],
    //平台id
    myAppId:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      myAppId:app.globalData.myAppId
    })

    if(options.paramObj){
      this.setData({
        paramObj: JSON.parse(options.paramObj),
      })
      //获取列表数据
      this.getList();

      //获取售后服务详情
      this.getAfterDetail();
    }
  },

  //获取列表数据
  getList(){
    Toast.loading({
      duration: 0,
      forbidClick: true,
      message: '加载中...'
    });

    let param = {
      isAuth: false,
      className: 'afterSaleParts',
      data: {
        logisticsId:this.data.paramObj.logisticsId,
        selectFields: ['id','logisticsId','factory','factoryName','part','partName','purchasingPrice','sealPrice','num','totalPurchasingPrice','totalSealPrice'],
        page: {
          page: this.data.page,
          pageSize: 10
        },
        nbl:Base64.encode(util.timeFormatDay(new Date()))
      }
    }

    CommonService.goFindList(param).then(res => {
      Toast.clear();
      if(res.data.data && res.data.data.result && res.data.data.result.length) {
        let result=res.data.data.result;
        console.log('配件列表');
        console.log(result);

        this.setData({
          list: this.data.list.concat(result)
        })
      }else{
        this.setData({
          notMore: false
        })
        if(this.data.page==1){
          this.setData({
            showNothing: true
          })
        }else{
          Toast.fail('没有更多了');
        }
      }
    }).catch((error) => {
      this.setData({
        showNothing: true
      })
    })
  },

  //刷新列表和页码
  refreshListAndPage(){
    this.setData({
      list: [],
      page:1,
      notMore: true,
      showNothing: false
    })
    this.getList();
  },

  //获取搜索词
  onChangeSearch(e) {
    this.setData({
      searchValue: e.detail,
    });

    if(!e.detail){
      //刷新列表和页码
      this.refreshListAndPage();
    }
  },

  //点击搜索
  onSearch(){
    this.refreshListAndPage();
  },

  //跳转添加/编辑
  goToAdd(e){
    let paramObj={
      logisticsId: this.data.paramObj.logisticsId,
      afterssrid: this.data.paramObj.afterssrid
    }

    if(e.currentTarget.dataset.detail){
      let detail=e.currentTarget.dataset.detail;
      paramObj.id=detail.id;
    }

    wx.navigateTo({
      url: "/pages/afterPartAdd/afterPartAdd?paramObj="+JSON.stringify(paramObj)
    })
  },

  //显示删除确认框
  onDelete(e){
    let detail=e.currentTarget.dataset.detail;
    this.setData({
      showDelete: true,
      deleteId:detail.id,
      deleteName:detail.partName
		})
  },

  //隐藏删除确认框
  onCloseDelete(){
    this.setData({
			showDelete: false
		})
  },

  //确认删除
  confirmDelete(){
    let param={
      className:"afterSaleParts",
      data:{
        id:this.data.deleteId
      }
    }

    CommonService.goDelete(param).then(res=>{
      if (res.statusCode == 200 && res.data.success == true){
        Toast.success('已删除');

        //售后记录增加详情
        this.insertAfterDetail();

        //删除费用记录
        this.deleteCost();

        //刷新列表和页码
        this.refreshListAndPage();
      }else{
        Toast.fail('操作失败');
      }
    }).catch((error) => {
      Toast.fail('操作失败');
    })
  },

  //获取售后服务详情
  getAfterDetail(){
    let param = {
      isAuth: false,
      className: 'afterSaleServiceRecord',
      data: {
        id:this.data.paramObj.afterssrid,
        selectFields: ['id','serviceDetails']
      }
    }

    CommonService.goFindList(param).then(res => {
      if(res.data.data && res.data.data.result && res.data.data.result.length) {
        let result=res.data.data.result;
        this.setData({
          serviceDetails: result[0].serviceDetails
        })
      }
    }).catch((error) => {
    })
  },

  //售后记录增加详情
  insertAfterDetail(){
    let td=this.data;
    let serviceDetails=td.serviceDetails;
    serviceDetails.push({
      content: "删除配件"+td.deleteName,
      datetime: util.timeFormat(new Date())
    });

    let param={
      className:'afterSaleServiceRecord',
      data:{
        id:td.paramObj.afterssrid,
        serviceDetails:serviceDetails
      }
    }

    CommonService.goSave(param).then(res=>{
    }).catch((error) => {
    })
  },

  //删除费用记录
  deleteCost(){
    let param={
      className:"afterSaleCostsRecord",
      data:{
        afterId:this.data.paramObj.afterssrid,
        itemId:this.data.deleteId
      }
    }

    CommonService.goDelete(param).then(res=>{
    }).catch((error) => {
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.notMore){
      this.setData({
        page: this.data.page + 1
      })
      this.getList();
    }
  }
})
